﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;
using Mimoza.DBLayer.Transport.Client;

namespace Mimoza.DBLayer.Transport.Server
{
    public class HostsRepository :
        ServerRepository<HostRecord, HostsRepositoryCustomRequest, IDBHostRepository>,
        IDBHostRepository
    {
        public HostsRepository(IDBHostRepository dbHosts,
            CollectionManager collectionManager, ConnectionList connList)
            : base(dbHosts, collectionManager, connList)
        {
            HostsRepositoryTranferRequest.RegisterInTransport();
            HostsRepositoryTranferRequest.Repository = this;
        }

        public HostRecord GetHost(Guid ID)
        {
            return BaseRepository.GetHost(ID);
        }

        public HostRecord GetHost(string MACAddress)
        {
            return BaseRepository.GetHost(MACAddress);
        }

        public HostRecord GetHostByName(string hostName)
        {
            return BaseRepository.GetHostByName(hostName);
        }

        override public void Insert(HostRecord Host)
        {
            BaseRepository.Insert(Host);
        }

        override public void Update(HostRecord Host)
        {
            BaseRepository.Update(Host);
        }

        override public void Delete(HostRecord Host)
        {
            BaseRepository.Delete(Host);
        }

        public override TransferObject ProcessRequest(Connection conn, HostsRepositoryCustomRequest request)
        {
            TransferObject reply = null;
            switch (request.OperationType)
            {
                case HostsRepositoryCustomRequest.CustomOperationType.GetHost:
                    reply = GetHost(request);
                    break;
                case HostsRepositoryCustomRequest.CustomOperationType.GetHostByMac:
                    reply = GetHostByMac(request);
                    break;
            }

            return reply;
        }

        protected TransferObject GetHost(HostsRepositoryCustomRequest request)
        {
            HostRecord record = GetHost(new Guid(request.Argument1));

            return new RepositoryReply<HostRecord>(record);
        }

        protected TransferObject GetHostByMac(HostsRepositoryCustomRequest request)
        {
            HostRecord record = GetHost(request.Argument1);

            return new RepositoryReply<HostRecord>(record);
        }
    }
}
